﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DataGridViewAutoFilter;
using ProrabHelp.Library;

namespace ProrabHelp.UI
{
    public partial class UsersManagement : UserControl
    {
        ServerObject mServerObject = null;
        bool mNewRecord = false;

        public UsersManagement()
        {
            InitializeComponent();
            mServerObject = new ServerObject();
        }

        private void newButton_Click(object sender, EventArgs e)
        {
            tableLayoutPanel1.SetColumnSpan(usersListGridView, 4);
            tableLayoutPanel2.Visible = true;

            userIDTextBox.Text = string.Empty;
            firstNameTextBox.Text = string.Empty;
            lastNameTextBox.Text = string.Empty;
            loginTextBox.Text = string.Empty;
            passwordTextBox.Text = string.Empty;
            roleTypeComboBox.SelectedIndex = -1;
            passwordStatusComboBox.SelectedIndex = -1;

            mNewRecord = true;
        }

        private void editButton_Click(object sender, EventArgs e)
        {
            EditFunction();
        }

        private void usersListGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            EditFunction();
        }

        private void EditFunction()
        {
            tableLayoutPanel1.SetColumnSpan(usersListGridView, 4);
            tableLayoutPanel2.Visible = true;

            if (usersListGridView.SelectedRows.Count == 1)
            {
                userIDTextBox.Text = usersListGridView.Rows[usersListGridView.SelectedRows[0].Index].Cells["UserID"].Value.ToString();
                firstNameTextBox.Text = usersListGridView.Rows[usersListGridView.SelectedRows[0].Index].Cells["FirstName"].Value.ToString();
                lastNameTextBox.Text = usersListGridView.Rows[usersListGridView.SelectedRows[0].Index].Cells["LastName"].Value.ToString();
                loginTextBox.Text = usersListGridView.Rows[usersListGridView.SelectedRows[0].Index].Cells["Login"].Value.ToString();
                passwordTextBox.Text = usersListGridView.Rows[usersListGridView.SelectedRows[0].Index].Cells["Password"].Value.ToString();

                if (usersListGridView.Rows[usersListGridView.SelectedRows[0].Index].Cells["isDelete"].Value.ToString().Equals("True"))
                {
                    isActiveUserCheckBox.Checked = false;
                }
                else
                {
                    isActiveUserCheckBox.Checked = true;
                }

                if (roleTypeComboBox.DataSource != null)
                {
                    roleTypeComboBox.SelectedValue = usersListGridView.Rows[usersListGridView.SelectedRows[0].Index].Cells["RoleType"].Value.ToString();
                }

                if (passwordStatusComboBox.DataSource != null)
                {
                    passwordStatusComboBox.SelectedValue = usersListGridView.Rows[usersListGridView.SelectedRows[0].Index].Cells["PasswordStatus"].Value.ToString();
                }
            }

            mNewRecord = false;
        }

        private void deleteButton_Click(object sender, EventArgs e)
        {
            if (usersListGridView.SelectedRows.Count == 1 && usersListGridView.RowCount > 1)
            {
                int UserID = 0;
                int.TryParse(usersListGridView.Rows[usersListGridView.SelectedRows[0].Index].Cells["UserID"].Value.ToString(), out UserID);
                if (UserID == 0 || !mServerObject.DeleteUser(UserID))
                {
                    MessageBox.Show("Sorry, record NOT deleted!");
                }

                FillUsersGridView();
            }
        }

        private void refreshButton_Click(object sender, EventArgs e)
        {
            FillUsersGridView();
        }

        private void FillUsersGridView()
        {
            DataTable dsource = mServerObject.GetUserList();
            BindingSource bindigSource = new BindingSource(dsource, null);
            usersListGridView.DataSource = bindigSource;
            usersListGridView.Columns["RoleType"].Visible = false;
            usersListGridView.Columns["PasswordStatus"].Visible = false;
            usersListGridView.Columns["UserID"].Visible = false;

            if (usersListGridView.DataSource != null)
            {
                foreach (DataGridViewColumn col in usersListGridView.Columns)
                {
                    col.HeaderCell = new DataGridViewAutoFilterColumnHeaderCell(col.HeaderCell);
                }

                usersListGridView.AutoResizeColumns();
            }

            if (dsource == null || dsource.Rows.Count == 0)
            { mNewRecord = true; }
        }

        private void UsersManagement_Load(object sender, EventArgs e)
        {
            FillRoleTypeComboBox();
            FillPasswordStatusComboBox();
            FillUsersGridView();

            tableLayoutPanel1.SetColumnSpan(usersListGridView, 5);
            tableLayoutPanel2.Visible = false;

            firstNameTextBox.Text = string.Empty;
            userIDTextBox.Text = string.Empty;
            lastNameTextBox.Text = string.Empty;
            loginTextBox.Text = string.Empty;
            passwordTextBox.Text = string.Empty;
            roleTypeComboBox.SelectedIndex = -1;
            passwordStatusComboBox.SelectedIndex = -1;
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            bool operatonResult = false;

            if (mNewRecord)
            {
                operatonResult = mServerObject.AddUser(firstNameTextBox.Text.Trim(),
                                                        lastNameTextBox.Text.Trim(),
                                                        loginTextBox.Text.Trim(),
                                                        passwordTextBox.Text.Trim(),
                                                        !isActiveUserCheckBox.Checked,
                                                        (int)roleTypeComboBox.SelectedValue,
                                                        (int)passwordStatusComboBox.SelectedValue);
            }
            else
            {
                int userID = 0;
                int.TryParse(userIDTextBox.Text, out userID);

                if (userID != 0)
                    operatonResult = mServerObject.UpdateUser(userID,
                                                            firstNameTextBox.Text.Trim(),
                                                            lastNameTextBox.Text.Trim(),
                                                            loginTextBox.Text.Trim(),
                                                            passwordTextBox.Text.Trim(),
                                                            !isActiveUserCheckBox.Checked,
                                                            (int)roleTypeComboBox.SelectedValue,
                                                            (int)passwordStatusComboBox.SelectedValue);
            }


            FillUsersGridView();

            tableLayoutPanel1.SetColumnSpan(usersListGridView, 5);
            tableLayoutPanel2.Visible = false;

            userIDTextBox.Text = string.Empty;
            firstNameTextBox.Text = string.Empty;
            lastNameTextBox.Text = string.Empty;
            loginTextBox.Text = string.Empty;
            passwordTextBox.Text = string.Empty;
            roleTypeComboBox.SelectedIndex = -1;
            passwordStatusComboBox.SelectedIndex = -1;

            mNewRecord = true;

            if (!operatonResult) MessageBox.Show("Sorry, Data not saved !");
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            tableLayoutPanel1.SetColumnSpan(usersListGridView, 5);
            tableLayoutPanel2.Visible = false;

            firstNameTextBox.Text = string.Empty;
            userIDTextBox.Text = string.Empty;
            lastNameTextBox.Text = string.Empty;
            loginTextBox.Text = string.Empty;
            passwordTextBox.Text = string.Empty;
            roleTypeComboBox.SelectedIndex = -1;
            passwordStatusComboBox.SelectedIndex = -1;
        }

        private void FillRoleTypeComboBox()
        {
            DataTable soureceTable = mServerObject.GetClassifiersListByTypeID(UFT.Table_TypeID.NAME_RoleType);

            if (soureceTable != null && soureceTable.Rows.Count > 0)
            {
                roleTypeComboBox.DataSource = soureceTable;
                roleTypeComboBox.DisplayMember = "Name";
                roleTypeComboBox.ValueMember = "CodeID";
                roleTypeComboBox.SelectedIndex = -1;
            }
        }

        private void FillPasswordStatusComboBox()
        {
            DataTable soureceTable = mServerObject.GetClassifiersListByTypeID(UFT.Table_TypeID.NAME_PasswordStatus);

            if (soureceTable != null && soureceTable.Rows.Count > 0)
            {
                passwordStatusComboBox.DataSource = soureceTable;
                passwordStatusComboBox.DisplayMember = "Name";
                passwordStatusComboBox.ValueMember = "CodeID";
                passwordStatusComboBox.SelectedIndex = -1;
            }
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            mServerObject.CloseThisTab(this); 
        }
    }
}
